import {defineStore} from "pinia";
import {AclApi} from "@/api/acl/index.js";

export const AclStores = defineStore('acl', {
    state: () => ({
        aclList: [],
        pageInfo: {
            page: 1,
            size: 10,
            total: 0,
            totalPage: 0
        }
    }),
    actions: {
        async list(params) {
            try {
                // 明确指定需要传递的参数，完全避免参数冲突
                const response = await AclApi.pagelist({
                    page: params.page || this.pageInfo.page,
                    size: params.size || this.pageInfo.size,
                    ...params
                    // 其他后端支持的参数可以在这里添加
                });
                console.log('请求参数:', params);

                console.log('响应数据:', response);


                if (response) {
                    this.aclList = Array.isArray(response.data?.records) ? response.data.records : [];
                    this.pageInfo = {
                        page: response.data.pageNumber || 1,
                        size: response.data.pageSize  || 10,
                        total:  response.data.totalRow || 0,
                        totalPage: response.data.totalPage ||  0
                    }
                    return response;
                }
            }
            catch (error) {
                console.error('请求错误:', error);
                throw error;
            }
        },
        //创建新权限
        async create(data) {
            try {
                const response = await AclApi.create(data);
                if (response) {
                    return response;
                }
            }
            catch (error) {
                console.error('创建失败:', error);
                throw error;
            }
        },
        //更新权限
        async update(data) {
            try {
                const response = await AclApi.update(data);
                if (response) {
                    return response;
                }
            } catch (error) {
                console.error('更新失败:', error);
                throw error;
            }
        },
        //逻辑删除权限
        async delete(id) {
            try {
                const response = await AclApi.delete(id);
                return response;
            } catch (error) {
                console.error('删除权限失败:', error);
                throw error;
            }
        }
    }
})
